import static pulpcore.image.Colors.rgb;
import pulpcore.animation.Fixed;
import pulpcore.image.CoreGraphics;
import pulpcore.image.CoreImage;
import pulpcore.scene.Scene2D;
import pulpcore.sprite.FilledSprite;
import pulpcore.sprite.Sprite;

import java.awt.Point;
import java.util.Scanner;


/* parameters for the map window should be:
 * x: (extraSpace * 2) + (NUM_TILES_AXIS_X + NUM_TILES_AXIS_Y) * halfTileWidth
 * y: (extraSpace * 4) + (NUM_TILES_AXIS_Y * 3) * halfTileHeight
 * 
 * This is assuming that the tiles are the right size.
 */
	
public class MapTest extends Scene2D {
	IsoMap map;
	
	static class IsoMap extends Sprite {
		private int tileWidth;
		private int tileHeight;
		private int segmentWidth;
		private int segmentHeight;
		private double tangent;
		private CoreImage[][] tileMap;
		private final int NUM_TILES_AXIS_X = 12;
		private final int NUM_TILES_AXIS_Y = 8;
		
		public IsoMap(/*CoreImage[][] tileMap,*/ int tileWidth, int tileHeight) {
			super(0, 0, tileWidth * 12, tileHeight * 8);
			this.tileWidth = tileWidth;
			this.tileHeight = tileHeight;
			this.segmentWidth = tileWidth / 2;
			this.segmentHeight = tileHeight / 2;
			this.tileMap = new CoreImage[1][1];
			tileMap[0][0] = CoreImage.load("WhiteSquare.png");
			this.tangent = getTileWidth() / getTileHeight();
			pixelSnapping.set(true);
		}
		
		@Override
		protected void drawSprite(CoreGraphics g) {
			int halfTileWidth = getTileWidth() / 2;
			int halfTileHeight = getTileHeight() / 2;
			int extraSpace = 50;
			int x = (NUM_TILES_AXIS_X + NUM_TILES_AXIS_Y - 1) * halfTileWidth + extraSpace;
			int y = extraSpace;
			
			for(int k = 1; k <= 3; k++) {
				for(int j = 0; j < NUM_TILES_AXIS_X; j++) {
					for (int i = 0; i < NUM_TILES_AXIS_Y; i++) {
						g.drawImage(tileMap[0][0], x, y);
						g.drawString(/*(NUM_TILES_AXIS_X - j)*/ /*(x) +*/ "," + (y) /*(NUM_TILES_AXIS_Y - i)*/, x, y);
						g.drawImage(CoreImage.load("KingFrontSmall.png"), x, y);
						x -= halfTileWidth;
						y += halfTileHeight;
					}
	
					// back to the original place
					x += NUM_TILES_AXIS_Y * halfTileWidth;
					y -= NUM_TILES_AXIS_Y * halfTileHeight;
	
					// shift to the next line
					x -= halfTileWidth;
				}
				
				x = (NUM_TILES_AXIS_X + NUM_TILES_AXIS_Y - 1) * halfTileWidth + extraSpace;
				y = (NUM_TILES_AXIS_Y * halfTileHeight + extraSpace) * k + extraSpace;
			}
		}
		
		public int getTileWidth() {
			return tileWidth;
		}
		
		public int getTileHeight() {
			return tileHeight;
		}
		
		public int getMapWidth() {
			return tileWidth * NUM_TILES_AXIS_X;
		}
		
		public int getMapHeight() {
			return tileHeight * NUM_TILES_AXIS_Y;
		}
	}	
	
	@Override
	public void load() {
		//background
		add(new FilledSprite(rgb(185, 209, 255)));
		
		//add IsoMap
		map = new IsoMap(50, 50);
		add(map);
		
		Board skyBoard = new Board(3);
		Board earthBoard = new Board(2);
		Board underBoard = new Board(1);
		
		skyBoard.add(new Griffin(1), new Square(3, 3, 8));
		skyBoard.add(new Griffin(2), new Square(3, 3, 1));
		skyBoard.add(new Griffin(1), new Square(3, 11, 8));
		skyBoard.add(new Griffin(2), new Square(3, 11, 1));
		skyBoard.add(new Dragon(1), new Square(3, 7, 8));
		skyBoard.add(new Dragon(2), new Square(3, 7, 1));
		skyBoard.add(new Sylph(1), new Square(3, 1, 7));
		skyBoard.add(new Sylph(2), new Square(3, 1, 2));
		skyBoard.add(new Sylph(1), new Square(3, 3, 7));
		skyBoard.add(new Sylph(2), new Square(3, 3, 2));
		skyBoard.add(new Sylph(1), new Square(3, 5, 7));
		skyBoard.add(new Sylph(2), new Square(3, 5, 2));
		skyBoard.add(new Sylph(1), new Square(3, 7, 7));
		skyBoard.add(new Sylph(2), new Square(3, 7, 2));
		skyBoard.add(new Sylph(1), new Square(3, 9, 7));
		skyBoard.add(new Sylph(2), new Square(3, 9, 2));
		skyBoard.add(new Sylph(1), new Square(3, 11, 7));
		skyBoard.add(new Sylph(2), new Square(3, 11, 2));
		earthBoard.add(new Oliphant(1), new Square(2, 1, 8));
		earthBoard.add(new Oliphant(2), new Square(2, 1, 1));
		earthBoard.add(new Oliphant(1), new Square(2, 12, 8));
		earthBoard.add(new Oliphant(2), new Square(2, 12, 1));
		earthBoard.add(new Unicorn(1), new Square(2, 2, 8));
		earthBoard.add(new Unicorn(2), new Square(2, 2, 1));
		earthBoard.add(new Unicorn(1), new Square(2, 11, 8));
		earthBoard.add(new Unicorn(2), new Square(2, 11, 1));
		earthBoard.add(new Hero(1), new Square(2, 3, 8));
		earthBoard.add(new Hero(2), new Square(2, 3, 1));
		earthBoard.add(new Hero(1), new Square(2, 10, 8));
		earthBoard.add(new Hero(2), new Square(2, 10, 1));
		earthBoard.add(new Thief(1), new Square(2, 4, 8));
		earthBoard.add(new Thief(2), new Square(2, 4, 1));
		earthBoard.add(new Thief(1), new Square(2, 9, 8));
		earthBoard.add(new Thief(2), new Square(2, 9, 1));
		earthBoard.add(new Cleric(1), new Square(2, 5, 8));
		earthBoard.add(new Cleric(2), new Square(2, 5, 1));
		earthBoard.add(new Mage(1), new Square(2, 6, 8));
		earthBoard.add(new Mage(2), new Square(2, 6, 1));
		earthBoard.add(new King(1), new Square(2, 7, 8));
		earthBoard.add(new King(2), new Square(2, 7, 1));
		earthBoard.add(new Paladin(1), new Square(2, 8, 8));
		earthBoard.add(new Paladin(2), new Square(2, 8, 1));
		earthBoard.add(new Warrior(1), new Square(2, 1, 7));
		earthBoard.add(new Warrior(2), new Square(2, 1, 2));
		earthBoard.add(new Warrior(1), new Square(2, 2, 7));
		earthBoard.add(new Warrior(2), new Square(2, 2, 2));
		earthBoard.add(new Warrior(1), new Square(2, 3, 7));
		earthBoard.add(new Warrior(2), new Square(2, 3, 2));
		earthBoard.add(new Warrior(1), new Square(2, 4, 7));
		earthBoard.add(new Warrior(2), new Square(2, 4, 2));
		earthBoard.add(new Warrior(1), new Square(2, 5, 7));
		earthBoard.add(new Warrior(2), new Square(2, 5, 2));
		earthBoard.add(new Warrior(1), new Square(2, 6, 7));
		earthBoard.add(new Warrior(2), new Square(2, 6, 2));
		earthBoard.add(new Warrior(1), new Square(2, 7, 7));
		earthBoard.add(new Warrior(2), new Square(2, 7, 2));
		earthBoard.add(new Warrior(1), new Square(2, 8, 7));
		earthBoard.add(new Warrior(2), new Square(2, 8, 2));
		earthBoard.add(new Warrior(1), new Square(2, 9, 7));
		earthBoard.add(new Warrior(2), new Square(2, 9, 2));
		earthBoard.add(new Warrior(1), new Square(2, 10, 7));
		earthBoard.add(new Warrior(2), new Square(2, 10, 2));
		earthBoard.add(new Warrior(1), new Square(2, 11, 7));
		earthBoard.add(new Warrior(2), new Square(2, 11, 2));
		earthBoard.add(new Warrior(1), new Square(2, 12, 7));
		earthBoard.add(new Warrior(2), new Square(2, 12, 2));
		underBoard.add(new Basilisk(1), new Square(1, 3, 8));
		underBoard.add(new Basilisk(2), new Square(1, 3, 1));
		underBoard.add(new Basilisk(1), new Square(1, 11, 8));
		underBoard.add(new Basilisk(2), new Square(1, 11, 1));
		underBoard.add(new Elemental(1), new Square(1, 7, 8));
		underBoard.add(new Elemental(2), new Square(1, 7, 1));
		underBoard.add(new Dwarf(1), new Square(1, 2, 7));
		underBoard.add(new Dwarf(2), new Square(1, 2, 2));
		underBoard.add(new Dwarf(1), new Square(1, 4, 7));
		underBoard.add(new Dwarf(2), new Square(1, 4, 2));
		underBoard.add(new Dwarf(1), new Square(1, 6, 7));
		underBoard.add(new Dwarf(2), new Square(1, 6, 2));
		underBoard.add(new Dwarf(1), new Square(1, 8, 7));
		underBoard.add(new Dwarf(2), new Square(1, 8, 2));
		underBoard.add(new Dwarf(1), new Square(2, 10, 7));
		underBoard.add(new Dwarf(2), new Square(2, 10, 2));
		underBoard.add(new Dwarf(1), new Square(2, 12, 7));
		underBoard.add(new Dwarf(2), new Square(2, 12, 2));
	}
	
	@Override
	public void update(int elapsedTime) {
		map.update(elapsedTime);
		
	}
}
